//
//  IGMessageModel.m
//  jingdongfang
//
//  Created by BOE on 2017/8/18.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGTopicModel.h"

@implementation IGTopicModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        _shouldUpdateCache = YES;
    }
    return self;
}

+ (JSONKeyMapper *)keyMapper
{
    return [[JSONKeyMapper alloc]
            initWithModelToJSONDictionary:@{
                                            @"tid":@"id"
                                            }];
}

- (FeedType)feedType {
    if (self.coverUrl && ![self.coverUrl isEqualToString:@""]) {
        return FeedTypeMovie;
    } else if (self.imgs.count > 0){
        return FeedTypeImage;
    }
    
    return FeedTypeInfo;
}

- (IGShareModel *)getShareModel {
    IGShareModel *shareModel = [IGShareModel new];
    shareModel.title = [NSString stringWithFormat:@"%@发布的话题", self.userNick];
    shareModel.webpageUrl = self.url;
    shareModel.descr = self.content.length>40?[self.content substringToIndex:39]:self.content;
    shareModel.thumbURL = self.imgs.count == 0?nil:self.imgs[0];
    
    return shareModel;
}

- (BOOL)containProduct {
    if (![self.product.artId isEqualToString:@""] && self.product.artId) {
        return YES;
    }
    return NO;
}

- (void)calVideoScreenRatioAndMovieUrl
{
    CGFloat width = [self.video.movieWidth floatValue];
    CGFloat height = [self.video.movieHeight floatValue];
//    if (self.resType == VideoResTypeSmall || (self.video.m3u8.count == 0 && self.video.mp4.count == 0)) {
//        self.movieUrl = self.video.movieUrl;
//    } else if (self.resType == VideoResTypeLong) {//优先m3u8 再mp4 320、640、848依次向上兼容
//        IGVideoTypeModel *model = [[IGVideoTypeModel alloc] init];
//        if (_video.m3u8.count != 0) {
//            [_video.m3u8 sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
//                IGVideoTypeModel *model1 = (IGVideoTypeModel *)obj1;
//                IGVideoTypeModel *model2 = (IGVideoTypeModel *)obj2;
//                if ([model1.vtype integerValue] < [model2.vtype integerValue]) {
//                    return NSOrderedAscending;
//                }
//                return NSOrderedDescending;
//            }];
//            NSMutableArray *muArr = [NSMutableArray arrayWithArray:_video.m3u8];
//            for (int i = 0; i < muArr.count - 1; i++) {
//                for (int j = i + 1; j < muArr.count; j++) {
//                    IGVideoTypeModel *model1 = [muArr objectAtIndex:i];
//                    IGVideoTypeModel *model2 = [muArr objectAtIndex:j];
//                    if ([model1.vtype intValue] > [model2.vtype intValue]) {
//                        [muArr exchangeObjectAtIndex:i withObjectAtIndex:j];
//                    }
//                }
//            }
//            model = [muArr objectAtIndex:0];
//        } else if (_video.m3u8.count == 0 && _video.mp4.count != 0) {
//            NSMutableArray *muArr = [NSMutableArray arrayWithArray:_video.mp4];
//            for (int i = 0; i < muArr.count - 1; i++) {
//                for (int j = i + 1; j < muArr.count; j++) {
//                    IGVideoTypeModel *model1 = [muArr objectAtIndex:i];
//                    IGVideoTypeModel *model2 = [muArr objectAtIndex:j];
//                    if ([model1.vtype intValue] > [model2.vtype intValue]) {
//                        [muArr exchangeObjectAtIndex:i withObjectAtIndex:j];
//                    }
//                }
//            }
//            model = [muArr objectAtIndex:0];
//        }
//
//        self.movieUrl = model.main_url;
//        width = [model.vwidth floatValue];
//        height = [model.vheight floatValue];
//    }
    self.resType = VideoResTypeSmall;
    self.movieUrl = self.video.movieUrl;
    
    if (width == height) {
        self.videoScreenRatio = VideoScreenRatioWEH;
    } else if (width > height) {
        self.videoScreenRatio = VideoScreenRatioWBH;
    } else if (width < height) {
        self.videoScreenRatio = VideoScreenRatioWSH;
    }
}

@end
